layui.use(['table', 'admin', 'ax'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;

    /**
     * 往来地址表管理
     */
    var BasicAddress = {
        tableId: "basicAddressTable"
    };

    /**
     * 初始化表格的列
     */
    BasicAddress.initColumn = function () {
        return [[
            {field: 'id', hide: true, title: '主键'},
            {field: 'putgoodsName', sort: true, title: '单位名称'},
            {field: 'address', sort: true, title: '地址'},
            {field: 'sheng', title: '省'},
            {field: 'shi', title: '市'},
            {field: 'qu', title: '区'},
            {field: 'contacts', sort: true, title: '联系人'},
            {field: 'tel', sort: true, title: '联系电话'},
            {field: 'longitude', title: '经度'},
            {field: 'latitude', title: '纬度'},
            {field: 'kilometer', sort: true, title: '公里数'},
            {align: 'center', width:70, toolbar: '#tableBar', title: '操作',fixed:'right'}
        ]];
    };
    
    //回填数据
    function confirm(data){
    	parent.submit = 1;
    	parent.AddressInfoDlg.data.setLocationId = data.id;
    	parent.AddressInfoDlg.data.setLocation = data.address;
    	parent.AddressInfoDlg.data.setPeople = data.contacts;
    	parent.AddressInfoDlg.data.setTel = data.tel;
    	var Index = parent.layer.getFrameIndex(window.name);
        parent.layer.close(Index);
    }

    /**
     * 点击查询按钮
     */
    BasicAddress.search = function () {
        var queryData = {};
        queryData['condition'] = $("#condition").val();
        table.reload(BasicAddress.tableId, {
            where: queryData, page: {curr: 1},
             done:function () {
            }
        });
    };

    /**
     * 导出excel按钮
     */
    BasicAddress.exportExcel = function () {
        var checkRows = table.checkStatus(BasicAddress.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + BasicAddress.tableId,
        url: Feng.ctxPath + '/basicAddress/list?putgoodsId='+Feng.getUrlParam("putgoodsId"),
        page: true,
        limit: 40,
        height: "full-60",
        toolbar: '#toolbarfrom',
        cellMinWidth: 100,
        cols: BasicAddress.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        BasicAddress.search();
    });

    // 导出excel
    $('#btnExp').click(function () {
        BasicAddress.exportExcel();
    });
    
    // 双击行事件
    table.on('rowDouble(' + BasicAddress.tableId + ')', function (obj) {
        var data = obj.data;
    	confirm(data);
    });

    // 工具条点击事件
    table.on('tool(' + BasicAddress.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;
        if (layEvent === 'confirm') {
        	confirm(data);
        } 
    });
});
